﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace Mindfor.Web.Models
{
	/// <summary>
	/// Represents collection of Minfor.Cms.Mvc.PageTitleNode for creating bread crumbs.
	/// </summary>
	public class PageTitleCollection : Collection<PageTitleNode>
	{
		/// <summary>
		/// Adds new page title node.
		/// </summary>
		/// <param name="text">Title node text.</param>
		/// <param name="url">Custom url.</param>
		/// <returns>New page title node.</returns>
		public PageTitleNode AddViewTitle(string text, string url = null)
		{
			PageTitleNode node = new PageTitleNode(text, true);
			if (!String.IsNullOrEmpty(url))
				node.Url = url;
			this.Add(node);
			return node;
		}

		/// <summary>
		/// Removes last title element.
		/// </summary>
		public void RemoveLast()
		{
			int last = this.Count - 1;
			if (last >= 0)
				RemoveAt(last);
		}

		/// <summary>
		/// Returns items that should be rendered. Skips items that overriden in view.
		/// </summary>
		public IEnumerable<PageTitleNode> GetRenderItems()
		{
			for (int i=0; i<Items.Count-1; i++)
			{
				PageTitleNode item = Items[i];
				PageTitleNode next = Items[i+1];
				if (!item.IsAddedFromView && next.IsAddedFromView)
					continue; // skip item if view replaced it
				yield return item;
			}
			if (Items.Count > 0)
				yield return Items[Items.Count-1];
		}
	}
}
